const app = getApp();
const cwx = require('cloudfile');
Page({
  data: {
    vis: false
  },
  postlistener(e) {
    var that = this;
    //发布页面监听 Promise 文件上传
    this.setData({
      vis: true
    })
    let postdata = e.detail;
    console.log(postdata)
    let images = postdata.images;
    var promisetasks = []
    for (var i = 0; i < images.length; i++) {
      promisetasks.push(cwx.CloudUploadImage(images[i]))
    }
    let postimages = []
    Promise.all(promisetasks).then(resarr => {
      console.log(resarr)
      resarr.forEach(function (ele, index) {
        postimages.push(ele.fileID)
      })
      console.log(postimages)
      //具体处理写在如下
      let userInfo = app.globalData.userInfo || wx.getStorageSync('userInfo')
      wx.cloud.callFunction({
        name: 'post',
        data: {
          action: 'add',
          data: {
            content: postdata.content,
            location: postdata.location,
            postimages: postimages,
            userInfo
          }
        },
        success: r => {
          console.log(r)
          this.setData({
            succMsg: '上传成功'
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 0,
            })
          }, 1500);
        },
        fail: r => {
          console.log(r)
          this.setData({
            errMsg: r.errMsg
          })
        },
        complete: r => {
          that.setData({
            vis: false
          })
        }
      })
    })
  },
  onLoad: function (options) {
    this.setData({
      envVersion: __wxConfig.envVersion
    })
  },
  onShareAppMessage: function () {}
})